package cn.tedu.egweibo06.controller;

import cn.tedu.egweibo06.common.response.JsonResult;
import cn.tedu.egweibo06.mapper.WeiboMapper;
import cn.tedu.egweibo06.pojo.dto.WeiboDTO;
import cn.tedu.egweibo06.pojo.entity.Weibo;
import cn.tedu.egweibo06.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
@Validated
@Api(tags = "02.微博模块")
@Slf4j
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @ApiOperation("发表微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody @Validated WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.debug("weiboDTO" + weiboDTO);
        UserVO userVO = (UserVO)session.getAttribute("user");
        log.debug("userVO" + userVO);
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setUserId(userVO.getId());
        weibo.setCreated(new Date());
        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }
    @ApiOperation("微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex(){
        return JsonResult.ok(weiboMapper.selectIndex());
    }
    @ApiOperation("根据微博id查询微博详情")
    @GetMapping("selectById")
    @ApiImplicitParam(name = "id",value = "微博编号",required = true, dataType = "int",example = "200")
    public JsonResult selectById(@Range(min = 200,max = 299,message = "微博id范围在200-299之间") @RequestParam int id){
        return JsonResult.ok(weiboMapper.selectById(id));
    }
}
